package com.fan_main;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.htmlparser.Node;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class DynamicAddThread {
	
	public void addUrlFromFile(final MyMain myMain,File file){
		if(file.exists()){
			if(file.isDirectory()){
				for(File f:file.listFiles()){
					addUrl2(myMain, f);
				}
			}else{
				addUrl2(myMain, file);
			}
		}
	}
	public void addUrl2(final MyMain myMain,File file) {
		try {
			if(!file.getName().endsWith(".doc")) return;
			String fileName=file.getName().substring(0, file.getName().lastIndexOf("."));
			JSONArray jarr=new JSONArray(); 
			FileInputStream in = new FileInputStream(file);// 载入文档
			POIFSFileSystem pfs = new POIFSFileSystem(in);
			HWPFDocument hwpf = new HWPFDocument(pfs);			
			Range range = hwpf.getRange();// 得到文档的读取范围
			TableIterator it = new TableIterator(range);
			// 迭代文档中的表格
			while (it.hasNext()) {
				Table tb = (Table) it.next();
				// 迭代行，默认从0开始
				String webName=fileName;
				for (int i = 0; i < tb.numRows(); i++) {
					TableRow tr = tb.getRow(i);
					if(i==0 && !tr.getCell(0).text().startsWith("http")) continue;
					JSONObject obj=new JSONObject();
					// 迭代列，默认从0开始
					TableCell td = tr.getCell(0);// 取得单元格
					// 取得单元格的内容					 
					String s=td.text();
					if(tr.numCells()==1){
						obj.put("webname",webName+i);
						obj.put("webSite", s.trim());
						obj.put("filter", tr.getCell(1).text().trim());
					}else{
						/*td = tr.getCell(1);
						for(int k=0;k<td.numParagraphs();k++){     
                            Paragraph para =td.getParagraph(k);     
                            String s2 = para.text().trim();     
                            System.out.println(s2);  
                        } */
						obj.put("webname",webName+tr.getCell(0).text().trim());
						obj.put("website", tr.getCell(1).text().trim());
						obj.put("encode", tr.getCell(2).text().trim());
						obj.put("filter", tr.getCell(3).text().trim());
						obj.put("urlLocation", tr.getCell(4).text().trim());
						obj.put("absolute", tr.getCell(5).text().trim());
						obj.put("timeLocation", tr.getCell(6).text().trim());
						obj.put("timeFormat", tr.getCell(7).text().trim());
					}
					jarr.add(obj);
				} // end for
			} // end while
			for(int i=0;i<jarr.size();i++){
				JSONObject obj=jarr.getJSONObject(i);
				String webname=obj.getString("webname");
				String website=obj.getString("website");
				final String encode=StringUtils.isBlank(obj.getString("encode"))?"utf-8":obj.getString("encode");
				final String filter=obj.getString("filter");
				final String urlLocationStr=obj.getString("urlLocation");
				final String absolute=obj.getString("absolute");
				final String timeLocationStr=obj.getString("timeLocation");
				final String timeFormatStr=obj.getString("timeFormat");
				
				new MyThread(webname,website) {
					@Override
					void method(String str) {
						while (stack.size() != 0) {
							 myMain.addrec(stack.pop());
						}
					}
					
					@Override
					public String downMethod() {
						try {
							String HTML = downHTML(encode);
							String filterArr[]=filter.split(" ").length==1?new String[]{filter.split(" ")[0],"",""}:filter.split(" ");
							if(filterArr.length>3){
								filterArr[2]+=" "+filterArr[3];
							}
							//NodeList nodes = analysHTML(HTML, "a", "class", "url");
							NodeList nodes = analysHTML(HTML, filterArr[0].trim(), filterArr[1].trim(), filterArr[2].trim());
							int times = this.gettimtes(1);

							for (int i = 0; i < nodes.size(); i++) {								
								try{
									Node node = nodes.elementAt(i);
								
								String nodeLocation[]=urlLocationStr.split("-");
								String timeLocation[]=timeLocationStr.split("-");								
								for(int j=0;j<nodeLocation.length;j++){
									if(nodeLocation[j].trim().equalsIgnoreCase("self")){
										//node=node;
									}else if(nodeLocation[j].trim().equalsIgnoreCase("firstChild")){
										node=node.getFirstChild();
									}else if(nodeLocation[j].trim().equalsIgnoreCase("lastChild")){
										node=node.getLastChild();
									}else if(nodeLocation[j].trim().trim().equalsIgnoreCase("next")){
										node=node.getNextSibling();
									}else if(nodeLocation[j].trim().equalsIgnoreCase("previous")){
										node=node.getPreviousSibling();
									}else if(nodeLocation[j].trim().equalsIgnoreCase("parent")){
										node=node.getParent();
									}
								}

								String title = node.toPlainTextString();
								TagNode tagnode = new TagNode();
								tagnode.setText(node.toHtml());
								String link = tagnode.getAttribute("href");
								Node timeNode=nodes.elementAt(i);
								for(int j=0;j<timeLocation.length;j++){
									if(timeLocation[j].trim().equalsIgnoreCase("self")){
										
									}else if(timeLocation[j].trim().equalsIgnoreCase("firstChild")){
										timeNode=timeNode.getFirstChild();
									}else if(timeLocation[j].trim().equalsIgnoreCase("lastChild")){
										timeNode=timeNode.getLastChild();
									}else if(timeLocation[j].trim().equalsIgnoreCase("next")){
										timeNode=timeNode.getNextSibling();
									}else if(timeLocation[j].trim().equalsIgnoreCase("previous")){
										timeNode=timeNode.getPreviousSibling();
									}else if(timeLocation[j].trim().equalsIgnoreCase("parent")){
										timeNode=timeNode.getParent();
									}
								}
								String basePath="";
								if(!"true".equals(absolute.trim())){
									basePath=this.website.substring(0, this.website.indexOf(new URL(this.website).getPath()))+"/";
									if(!"false".equals(absolute.trim())){
										basePath+=absolute.trim();
									}
								}
								link = basePath	+ link;								
								String time =formatTime(timeNode.toPlainTextString().replace("[", "").replace("]", "").replace("(", "").replace(")","").replace("　", "").trim(),timeFormatStr.trim());
									//link.substring(0, 7)+"/"+link.substring(7, 9);
								Record rec = new Record();
								rec.setLink(link);
								rec.setTime(time);
								rec.setTitle(title);

								if (rec.totime() >= times) {									
									if(!myMain.model.contains(rec)){
										stack.add(rec);
									}
								} else {
									break;
								}
							}catch (Exception e) {
								e.printStackTrace();
							}
							}							
						} catch (ParserException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (MalformedURLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						return null;
					}
				
				};
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}// end method
	String formatTime(String source,String pattern){
		DateFormat df=null;
		try {
			if(!source.contains(":") && pattern.contains(":")){
				pattern=pattern.substring(0, pattern.indexOf(" "));
			}else if(pattern.equals("MM-dd")){
				source=Calendar.getInstance().get(Calendar.YEAR)+"-"+source;
				pattern="yyyy-"+pattern;
			}
			df=new SimpleDateFormat(pattern);
			Date d=df.parse(source);
			return new SimpleDateFormat("yyyy-MM-dd").format(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return source;
	}
}
